// JavaScript Document
$(document).ready(function(){
	$('.reserve').click(function (e){
		e.preventDefault();
		if($(this).parent().parent().find('input[type="checkbox"]').is(':checked')){
			$(this).parent().parent().find('input[type="checkbox"]').removeAttr('checked');
			$(this).html('Click to reserve');
		}
		else {
			$(this).parent().parent().find('input[type="checkbox"]').attr('checked', 'checked');
			$(this).html('Cancel reservation')
		}
		
	})
	
	$('.tpicker').ptTimeSelect();;
	
	$('.datepicker').Zebra_DatePicker({
		direction: 1,
		show_icon: false
	});;
	
	$('.remove').click(function (){
		$(this).parent().parent().hide().remove();	
	});
	
	$('#save_reservation').submit(function (){
		var okFlag = 1;
		var emptyFlag = 1;
		var subjFlag = 1;
		var timeFlag = 1;
		var emptyMsg = '';
		var dateMsg = '';
		var subjMsg = '';
		var timeMsg = '';
		
		
		$('.tbl-row').each(function(index, element) {
			$(this).removeClass('invalid-date').removeClass('blank-date');
			$(this).find('.subject').removeClass('required');
			$(this).find('.tpicker').removeClass('required');
			var from = $(this).find('.from').val();
			var to = $(this).find('.to').val();
			
			if($(this).find('.subject').val() == ''){
				$(this).find('.subject').addClass('required');
				subjFlag = 0;
				subjMsg = 'Please fill up the requiring subject \n\n';
			}
			
			if(from == '' || to == ''){
				$(this).addClass('blank-date');
				emptyFlag = 0;	
				emptyMsg = 'Please fill in your reservation date \n\n';
				
			}
			
			var dateFrom = new Date(from);
			var dateTo = new Date(to);
			
			if(dateTo < dateFrom) {
				$(this).addClass('invalid-date');
				okFlag = 0;
				dateMsg = 'Please check your reservation dates: \n\n \t\t\t TO date is less than FROM date';
			}
			

			if($(this).find('.tpicker').val() == ''){
				
				$(this).find('.tpicker').addClass('required');
				timeFlag = 0;	
				timeMsg = 'Please fill in your pickup time \n\n';
				
			}
			
		});
		if(okFlag && emptyFlag && timeFlag){
			return true;
		}
		else{
			alert(subjMsg+emptyMsg+dateMsg+timeMsg);
			return false;	
		}
			
	});
	
});
